/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package sg.gde.ScriptManager;

import java.util.logging.Logger;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ui.OpenProjects;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.filesystems.FileObject;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import sg.gde.ScriptManager.control.NetbeanGroovyScriptEngine;
import sg.gde.ScriptManager.view.ScriptBaseTopComponent;
import sg.gde.atomcore.notify.NotifyUtil;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//sg.gde.ScriptManager//ScriptManager//EN",
autostore = false)
@TopComponent.Description(preferredID = "ScriptManagerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "output", openAtStartup = false)
@ActionID(category = "Window", id = "sg.gde.ScriptManager.ScriptManagerTopComponent")
@ActionReference(path = "Menu/Window" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_ScriptManagerAction",
preferredID = "ScriptManagerTopComponent")
public final class ScriptManagerTopComponent extends TopComponent {

    private NetbeanGroovyScriptEngine scriptEngine;
    private Project proj;

    public ScriptManagerTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(ScriptManagerTopComponent.class, "CTL_ScriptManagerTopComponent"));
        setToolTipText(NbBundle.getMessage(ScriptManagerTopComponent.class, "HINT_ScriptManagerTopComponent"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tblScripts1 = new javax.swing.JTable();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane7 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jCheckBox1 = new javax.swing.JCheckBox();
        btnAddTopComponent = new javax.swing.JButton();
        btnStart = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        EngineListPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbEngineList = new javax.swing.JComboBox();
        btnActive = new javax.swing.JToggleButton();
        btnOption = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        btnRunScript = new javax.swing.JButton();
        btnCheck = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        btnLoadScript = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        lblStatus = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtCode = new javax.swing.JEditorPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtOutput = new javax.swing.JEditorPane();
        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btnAddBinding = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        btnActiveToggle = new javax.swing.JButton();
        btnLink = new javax.swing.JButton();
        btnLinkScript = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        tblScripts1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Id", "Script", "TopComponent", "Status"
            }
        ));
        jScrollPane5.setViewportView(tblScripts1);
        tblScripts1.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title0")); // NOI18N
        tblScripts1.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title1")); // NOI18N
        tblScripts1.getColumnModel().getColumn(2).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title2")); // NOI18N
        tblScripts1.getColumnModel().getColumn(3).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title3")); // NOI18N

        jScrollPane7.setViewportView(jTree1);

        org.openide.awt.Mnemonics.setLocalizedText(jButton6, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jButton6.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton7, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jButton7.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton8, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jButton8.text")); // NOI18N

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane7)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton7))
                    .addComponent(jButton8))
                .addContainerGap(66, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(64, 64, 64))
        );

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jLabel3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jLabel4.text")); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane6.setViewportView(jList1);

        org.openide.awt.Mnemonics.setLocalizedText(jCheckBox1, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jCheckBox1.text")); // NOI18N
        jCheckBox1.setToolTipText(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jCheckBox1.toolTipText")); // NOI18N
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnAddTopComponent, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnAddTopComponent.text")); // NOI18N
        btnAddTopComponent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTopComponentActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnStart, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnStart.text")); // NOI18N
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnStop, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnStop.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jRadioButton1, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jRadioButton1.text")); // NOI18N

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(btnStart))
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox1)
                            .addComponent(jRadioButton1)))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStop)
                        .addGap(85, 85, 85)
                        .addComponent(btnAddTopComponent)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jCheckBox1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jRadioButton1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAddTopComponent)
                            .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnStart)
                                .addComponent(btnStop)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        jPanel1.setLayout(new java.awt.BorderLayout(4, 4));

        jPanel7.setLayout(new javax.swing.BoxLayout(jPanel7, javax.swing.BoxLayout.PAGE_AXIS));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jLabel1.text")); // NOI18N

        cbEngineList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.openide.awt.Mnemonics.setLocalizedText(btnActive, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnActive.text")); // NOI18N
        btnActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnActiveActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnOption, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnOption.text")); // NOI18N

        javax.swing.GroupLayout EngineListPanelLayout = new javax.swing.GroupLayout(EngineListPanel);
        EngineListPanel.setLayout(EngineListPanelLayout);
        EngineListPanelLayout.setHorizontalGroup(
            EngineListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(EngineListPanelLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1)
                .addGap(89, 89, 89)
                .addComponent(cbEngineList, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnActive, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnOption)
                .addContainerGap(45, Short.MAX_VALUE))
        );
        EngineListPanelLayout.setVerticalGroup(
            EngineListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(EngineListPanelLayout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(jLabel1))
            .addGroup(EngineListPanelLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(EngineListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnActive)
                    .addComponent(cbEngineList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnOption)))
        );

        btnActive.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jToggleButton1.AccessibleContext.accessibleName")); // NOI18N

        jPanel7.add(EngineListPanel);

        org.openide.awt.Mnemonics.setLocalizedText(btnRunScript, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnRunScript.text")); // NOI18N
        btnRunScript.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRunScriptActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnCheck, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnCheck.text")); // NOI18N
        btnCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jButton2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(btnLoadScript, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnLoadScript.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(102, 102, 102)
                .addComponent(btnRunScript)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCheck)
                .addGap(56, 56, 56)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLoadScript, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(417, 417, 417))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCheck)
                    .addComponent(btnRunScript)
                    .addComponent(jButton2)
                    .addComponent(btnLoadScript)
                    .addComponent(jLabel2)))
        );

        jPanel7.add(jPanel4);

        jPanel1.add(jPanel7, java.awt.BorderLayout.PAGE_START);

        jPanel6.setLayout(new java.awt.GridLayout(1, 0));

        lblStatus.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        org.openide.awt.Mnemonics.setLocalizedText(lblStatus, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.lblStatus.text")); // NOI18N
        jPanel6.add(lblStatus);
        jPanel6.add(jProgressBar1);

        jPanel1.add(jPanel6, java.awt.BorderLayout.SOUTH);

        jPanel5.setLayout(new java.awt.GridLayout(2, 1));

        txtCode.setBackground(java.awt.SystemColor.activeCaptionText);
        txtCode.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        txtCode.setFont(new java.awt.Font("Courier New", 0, 11)); // NOI18N
        txtCode.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jScrollPane3.setViewportView(txtCode);

        jPanel5.add(jScrollPane3);

        txtOutput.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        txtOutput.setContentType(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.txtOutput.contentType")); // NOI18N
        jScrollPane2.setViewportView(txtOutput);

        jPanel5.add(jScrollPane2);

        jPanel1.add(jPanel5, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        org.openide.awt.Mnemonics.setLocalizedText(btnAddBinding, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnAddBinding.text")); // NOI18N
        btnAddBinding.setFocusable(false);
        btnAddBinding.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAddBinding.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnAddBinding);

        org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        org.openide.awt.Mnemonics.setLocalizedText(btnActiveToggle, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnActiveToggle.text")); // NOI18N
        btnActiveToggle.setFocusable(false);
        btnActiveToggle.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnActiveToggle.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnActiveToggle);

        org.openide.awt.Mnemonics.setLocalizedText(btnLink, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnLink.text")); // NOI18N
        btnLink.setFocusable(false);
        btnLink.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLink.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnLink.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLinkActionPerformed(evt);
            }
        });
        jToolBar1.add(btnLink);

        org.openide.awt.Mnemonics.setLocalizedText(btnLinkScript, org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.btnLinkScript.text")); // NOI18N
        btnLinkScript.setFocusable(false);
        btnLinkScript.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnLinkScript.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnLinkScript);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Var", "Binding Name", "Script", "Status"
            }
        ));
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title1")); // NOI18N
        jTable1.getColumnModel().getColumn(2).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title2")); // NOI18N
        jTable1.getColumnModel().getColumn(3).setHeaderValue(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jTable1.columnModel.title3")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 722, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 872, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(96, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(ScriptManagerTopComponent.class, "ScriptManagerTopComponent.jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 887, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnLinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLinkActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_btnLinkActionPerformed

private void btnActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActiveActionPerformed
    // TODO add your handling code here:

    cleanAllComponent();
}//GEN-LAST:event_btnActiveActionPerformed

private void btnRunScriptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRunScriptActionPerformed
    // TODO add your handling code here:
    if (scriptEngine != null) {
        String text = txtCode.getText();
        if (!text.trim().equals("")) {
            lblStatus.setText("Status: Running ...");
            scriptEngine.eval(text);
            //txtOutput.setText(scriptEngine.getAllCodeLines());
            lblStatus.setText("Status: Finished!");
        } else {
            lblStatus.setText("Error: You have to enter code !");
        }
    } else {
        lblStatus.setText("Error: Start Script Engine first. Click 'Active' Button !");
    }
}//GEN-LAST:event_btnRunScriptActionPerformed

private void btnCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckActionPerformed
    // TODO add your handling code here:
    /*
    String text = txtCode.getText();
    if (!text.trim().equals("")) {

        boolean test = scriptEngine.check(text);
        if (test) {
            lblStatus.setText("Status: Syntax test passed!");
        } else {
            lblStatus.setText("Status: Syntax test failed!");
        }
    } else {
        lblStatus.setText("Error: You have to enter code !");
    
    * */
}//GEN-LAST:event_btnCheckActionPerformed

    private void btnAddTopComponentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTopComponentActionPerformed
        // TODO add your handling code here:

        ScriptBaseTopComponent tc = new ScriptBaseTopComponent(scriptEngine, "new Script");
        tc.open("Cal.groovy");
    }//GEN-LAST:event_btnAddTopComponentActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        // TODO add your handling code here:
        if (checkOpenedProject()) {
            startScriptEngineInProject();
        } else {
            NotifyUtil.warn("title", "Can not find .Script folder ", false);
        }
    }//GEN-LAST:event_btnStartActionPerformed

    public void startScriptEngineInProject() {
        FileObject root = proj.getProjectDirectory();
        String path = root.getPath();
        Logger.getLogger("ScriptEngine").info("The script path :" + path);
        scriptEngine = new NetbeanGroovyScriptEngine();
        scriptEngine.startEngine(path + "/src/script/");
    }
    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBox1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel EngineListPanel;
    private javax.swing.JToggleButton btnActive;
    private javax.swing.JButton btnActiveToggle;
    private javax.swing.JButton btnAddBinding;
    private javax.swing.JButton btnAddTopComponent;
    private javax.swing.JButton btnCheck;
    private javax.swing.JButton btnLink;
    private javax.swing.JButton btnLinkScript;
    private javax.swing.JButton btnLoadScript;
    private javax.swing.JButton btnOption;
    private javax.swing.JButton btnRunScript;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStop;
    private javax.swing.JComboBox cbEngineList;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree jTree1;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JTable tblScripts1;
    private javax.swing.JEditorPane txtCode;
    private javax.swing.JEditorPane txtOutput;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        checkOpenedProject();
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    private void cleanAllComponent() {
        txtCode.setText("");
        txtOutput.setText("");

    }

    private boolean checkOpenedProject() {
        //Lookup utilLookup = Utilities.actionsGlobalContext();
        //proj = utilLookup.lookup(Project.class);
        Project[] openProjects = OpenProjects.getDefault().getOpenProjects();
        if (OpenProjects.getDefault().getMainProject() != null) {
            proj = OpenProjects.getDefault().getMainProject();
            return true;
        } else if (openProjects != null & openProjects.length != 0) {

            proj = openProjects[0];
            return true;
        } else {
            return false;
        }

    }
}
